package cn.edu.ahstu.acm.context;

import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class OJConfig {

	public String dataFile;
	public String tempFile;
	public int timeLimit;
	public int memoryLimit;
	public String buildPath;
	public Map<Integer, Language> langs = new LinkedHashMap<Integer, Language>();
	public List<String> languages = new ArrayList<String>();
	private String configXml = "../properties/judgecore.xml";
	public static OJConfig instance = new OJConfig();

	private OJConfig() {
		loadResource();
		init();
	}

	private void init() {

	}

	public void loadResource() {
		SAXReader saxReader = new SAXReader();
		try {
			URL url = getClass().getClassLoader().getResource(configXml);
			Document document = saxReader.read(url);
			Element root = document.getRootElement();
			if (root.getName().equalsIgnoreCase("config")) {
				Element defaultElem = root.element("default");
				if (defaultElem != null) {
					dataFile = defaultElem.elementText("data");
					tempFile = defaultElem.elementText("temp");
					timeLimit = Integer.parseInt(defaultElem
							.elementText("timelimit"));
					memoryLimit = Integer.parseInt(defaultElem
							.elementText("memorylimit"));
				//	buildPath = defaultElem.elementText("buildpath");
				}
				Element langsElem = root.element("languages");
				if (langsElem != null) {
					int index = 0;
					for (Object obj : langsElem.elements()) {
						Element elem = (Element) obj;
						Language lang = new Language();
						lang.type = elem.attributeValue("type");
						lang.ext = elem.elementText("ext");
						lang.exe = elem.elementText("exe");
						lang.timelimit = Integer.parseInt(elem
								.elementText("timelimit"));
						lang.memorylimit = Integer.parseInt(elem
								.elementText("memorylimit"));
						lang.comshell = elem.elementText("comshell")
								.replaceAll("\\$\\{ext\\}", lang.ext)
								.replaceAll("\\$\\{exe\\}", lang.exe);
						lang.runshell = elem.elementText("runshell")
								.replaceAll("\\$\\{ext\\}", lang.ext)
								.replaceAll("\\$\\{exe\\}", lang.exe);
						languages.add(lang.type);
						langs.put(index++, lang);
					}
				}
			}
		} catch (DocumentException e) {
		}
	}

	public void setConfigXml(String configXml) {
		this.configXml = configXml;
	}

	public static class Language {
		public String type;
		public String ext;
		public String exe;
		public int timelimit;
		public int memorylimit;
		public String comshell;
		public String runshell;

		public String getType() {
			return type;
		}

		public String getExt() {
			return ext;
		}

		public int getTimelimit() {
			return timelimit;
		}

		public int getMemorylimit() {
			return memorylimit;
		}

		public String getComshell(String folder) {
			return comshell.replace("${path}", folder)
					.replace("${path}", folder).replace("${name}", "Main")
					.replace("${name}", "Main");
		}

		public String getRunshell(String folder) {
			return runshell.replace("${path}", folder)
					.replace("${path}", folder).replace("${name}", "Main")
					.replace("${name}", "Main");
		}

		@Override
		public String toString() {
			return "Language [ext=" + ext + ", runshell=" + runshell + "]";
		}
	}

	public static void main(String[] args) {
		OJConfig config = new OJConfig();
		config.setConfigXml("src\\main\\webapp\\WEB-INF\\properties\\judgecore.xml");
		config.init();
	}
}
